import { BadgeType } from "../../model/BadgeType";
import BaseUI from "../../core/gui/BaseUI";
import { ui } from "../../ui/layaMaxUI";
import HomeView from "./HomeView";
import BadgeManage from "../../core/badge/BadgeManage";
import { FlyGold } from "../../core/anim/FlyGold";
import { game } from "../../core/game";
import SpineFactory from "../../core/anim/SpineFactory";
import { BadgeAction } from "../../core/badge/BadgeNode";
import Tween from "../../core/anim/Tween";
import UIUtils from "../../core/utils/UIUtils";
import ColorUtils from "../../core/utils/ColorUtils";
import { Logger } from "../../core/log/logger";
import Utils from "../../core/utils/Utils";
import { GuideManage } from "../../core/guide/GuideManage";
import NetMask from "../../core/gui/NetMask";
import NetManage from "../../core/net/NetManage";


export default class LoginScene extends BaseUI<ui.main.LoginSceneUI> {


    private canClick = true;
    constructor() {
        super(ui.main.LoginSceneUI, LoginScene.GID());
    }

    onInit(): void {
        let badge = BadgeManage.I;
        let action: BadgeAction = { caller: this, method: this.badgeFun, ages: [1] }
        badge.addGropu(BadgeType.HOMEVIEW, [BadgeType.TEST1, BadgeType.TEST2, BadgeType.TEST3, BadgeType.TEST4,], action);

        action = { caller: this, method: this.badgeFun, ages: [2] }
        badge.addGropu(BadgeType.TEST1, [BadgeType.TEST3, BadgeType.TEST4,], action);

        action = { caller: this, method: this.badgeFun, ages: [3] }
        badge.addGropu(BadgeType.TEST2, [BadgeType.TEST3, BadgeType.TEST4,], action);

        action = { caller: this, method: this.badgeFun, ages: [4] }
        badge.addGropu(BadgeType.TEST3, [BadgeType.TEST4], action);

        action = { caller: this, method: this.badgeFun, ages: [5] }
        badge.add(BadgeType.TEST4, action);


        let view = this.view;
        this.prepare(view.imd1, BadgeType.HOMEVIEW);
        this.prepare(view.imd2, BadgeType.TEST1);
        this.prepare(view.imd3, BadgeType.TEST2);
        this.prepare(view.imd4, BadgeType.TEST3);
        this.prepare(view.imd5, BadgeType.TEST4);

        this.onClick(view.imd1, this.onImgClick, [1]);
        this.onClick(view.imd2, this.onImgClick, [2]);
        this.onClick(view.imd3, this.onImgClick, [3]);
        this.onClick(view.imd4, this.onImgClick, [4]);
        this.onClick(view.imd5, this.onImgClick, [5]);

        // console.log("开始编测试译脚本");

        let cenx = this.view.width >> 1, ceny = this.view.height >> 1;
        view.board.graphics.drawRect(cenx - 264, ceny - 264, 528, 528, ColorUtils.black, ColorUtils.white, 3);

        UIUtils.addClick(view.drag, () => {
            if (!this.canClick) return;
            console.log("点击");

        });
        view.drag.on(Laya.Event.DRAG_START, this, () => {
            this.canClick = false;
            // Tween.once(view.drag).to({ scaleX: 1.2, scaleY: 1.2 })
            console.log("开始拖动")
        })

        view.drag.on(Laya.Event.DRAG_END, this, () => {
            this.canClick = true;
            // Tween.once(view.drag).to({ scaleX: 1, scaleY: 1 })
            console.log("结束拖动");

        })

        view.drag.on(Laya.Event.DRAG_MOVE, this, (e) => {

            console.log(e);

        })

        view.drag.on(Laya.Event.MOUSE_DOWN, this, () => {

            let rect = Laya.Rectangle.create().setTo(cenx - 200, ceny - 200, 400, 400);

            view.drag.startDrag(rect, true, 100, 100, null, true, 0.95)
        })

        view.drag.on(Laya.Event.MOUSE_UP, this, () => {
            view.drag.stopDrag();
        })

    }

    onOpen(v: any) {
        console.log("onOpen");
    }

    private popRes(start) {
        let fly = FlyGold.Instance();
        fly.popRes("images/common/coin.png", start, "获得奖励")
    }


    private playCoin(start) {
        let fly = FlyGold.Instance();

        fly.flyRes("images/common/coin.png", start, [200, 50], 20, "coin");
    }

    private plyaStr(e: Laya.Event) {
        let fly = FlyGold.Instance();

        // fly.flyRes("images/common/coin.png", start, [0, 0], 20, "coin");
        fly.popRes("images/common/coin.png", e.target, "金币+1", "popitem");

        SpineFactory.I.playSk("images/boneAnim/common/bglight.sk", e.target);
        SpineFactory.I.create("images/boneAnim/zhuque.sk").setP(this.view, 200, 200)
            .play(0, false, "musics/accecar.mp3", 500)
            .wait(200)
            .play()
            .call(Laya.Handler.create(this, this.playCoin, [e.target]))
            .wait(300)
            .play()
            .dispose()

    }

    private onImgClick(type: number, e: Laya.Event) {
        let btn = e.currentTarget as Laya.Button;

        if (btn.dataSource == null || btn.dataSource == 1) {
            btn.dataSource = 0;
        } else {
            btn.dataSource = 1;
        }

        if (type == 1) {
            BadgeManage.I.event(BadgeType.HOMEVIEW)
        } else {
            BadgeManage.I.event(BadgeType[`TEST${type - 1}`])

        }
        switch (type) {
            case 1:
                game.ui.popup(HomeView, "参数");

                break;
            case 2:
                // game.ui.popup(HomeView, "参数");
                this.plyaStr(e)
                break;
            case 3:
                this.playCoin(e.target)
                NetManage.Instance().seend({ res: "测试" })
                break;
            case 4:
                this.popRes(e.target)
                break;
            case 5:
                NetMask.show();
                this.view.timer.once(400, this, () => NetMask.clear());
                // game.ui.popup(HomeView, "参数");
                // GuideManage.show()
                break;
        }

    }

    private badgeFun(type: number): number {
        let btn = this.view[`imd${type}`] as Laya.Button;
        if (!btn || !btn.dataSource) return 0;

        return btn.dataSource;

    }

}